package cn.edu.nju.freshdelivery.dao.impl;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.nju.freshdelivery.dao.BuyerDAO;
import cn.edu.nju.freshdelivery.model.Buyer;

/**
 * A data access object (DAO) providing persistence and search support for Buyer
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 * 
 * @see cn.edu.nju.freshdelivery.dao.impl.Buyer
 * @author MyEclipse Persistence Tools
 */

public class BuyerDAOImpl extends HibernateDaoSupport implements BuyerDAO {
	private static final Logger log = LoggerFactory.getLogger(BuyerDAOImpl.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String ADDRESS = "address";
	public static final String PHONE = "phone";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#save(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public void save(Buyer transientInstance) {
		log.debug("saving Buyer instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#delete(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public void delete(Buyer persistentInstance) {
		log.debug("deleting Buyer instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public void update(Buyer persistentInstance)
	{
		try {
			this.getHibernateTemplate().update(persistentInstance);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findById(java.lang.Integer)
	 */
	public Buyer findById(java.lang.Integer id) {
		log.debug("getting Buyer instance with id: " + id);
		try {
			Buyer instance = (Buyer) getHibernateTemplate().get(
					"cn.edu.nju.freshdelivery.model.Buyer", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByExample(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public List<Buyer> findByExample(Buyer instance) {
		log.debug("finding Buyer instance by example");
		try {
			List<Buyer> results = (List<Buyer>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Buyer instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Buyer as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByUsername(java.lang.Object)
	 */
	public List<Buyer> findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByPassword(java.lang.Object)
	 */
	public List<Buyer> findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByName(java.lang.Object)
	 */
	public List<Buyer> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByAddress(java.lang.Object)
	 */
	public List<Buyer> findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findByPhone(java.lang.Object)
	 */
	public List<Buyer> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Buyer instances");
		try {
			String queryString = "from Buyer";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#merge(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public Buyer merge(Buyer detachedInstance) {
		log.debug("merging Buyer instance");
		try {
			Buyer result = (Buyer) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#attachDirty(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public void attachDirty(Buyer instance) {
		log.debug("attaching dirty Buyer instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see cn.edu.nju.freshdelivery.dao.impl.BuyerDAO#attachClean(cn.edu.nju.freshdelivery.model.Buyer)
	 */
	public void attachClean(Buyer instance) {
		log.debug("attaching clean Buyer instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static BuyerDAO getFromApplicationContext(ApplicationContext ctx) {
		return (BuyerDAO) ctx.getBean("BuyerDAO");
	}
}